<template>
  <div class="categorise">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-button type="primary" @click="changeAddBl">添加分类</el-button>
      <tree-grid
        :data="catList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        border
        stripe
        show-index
        index-text="#"
        :show-row-hover="false">
          <template slot="cat_deleted" slot-scope="scope">
            <i class="el-icon-success" v-if="!scope.row.cat_deleted" style="color:#409eff"></i>
            <i class="el-icon-error" v-if="scope.row.cat_deleted" style="color:red"></i>
          </template>
          <template slot="cat_level" slot-scope="scope">
            <el-tag v-if="scope.row.cat_level == 0">等级一</el-tag>
            <el-tag v-if="scope.row.cat_level == 1" type="success">等级二</el-tag>
            <el-tag v-if="scope.row.cat_level == 2" type="warning">等级三</el-tag>
          </template>
          <template slot="cat_btn">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
          </template>
      </tree-grid>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryCatData.pagenum"
        :page-sizes="[2, 4, 6, 8, 10]"
        :page-size="queryCatData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background>
      </el-pagination>

      <el-dialog title="添加" :visible.sync="addDialogVisible" width="50%" @close="closeAddCateDialog">
        <el-form :model="addCatForm" ref="addRuleForm" label-width="120px" class="demo-ruleForm"  :rules="addRules">
          <el-form-item label="活动名称:" prop="cat_name">
            <el-input v-model="addCatForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类:">
            <el-cascader clearable v-model="cascaderValue" :options="parentCat" :props="cascaderProps" @change="cascaderChane"></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCateSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryCatData: {
        pagenum: 1,
        pagesize: 10
      },
      catList: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'cat_deleted'
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template',
          template: 'cat_level'
        },
        {
          label: '操作',
          prop: 'cat_btn',
          type: 'template',
          template: 'cat_btn',
          width: '200'
        }
      ],
      addDialogVisible: false,
      addCatForm: {
        cat_name: '',
        cat_level: 0,
        cat_pid: 0
      },
      addRules: {
        cat_name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      parentCat: [],
      cascaderValue: '',
      cascaderProps: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        checkStrictly: true
      }
    }
  },

  created () {
    this.getCatList()
  },

  methods: {
    async getCatList () {
      const { data: res } = await this.$axios.get('categories', {
        params: this.queryCatData
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.catList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.queryCatData.pagesize = val
      this.getCatList()
    },
    handleCurrentChange (val) {
      this.queryCatData.pagenum = val
      this.getCatList()
    },
    async changeAddBl () {
      this.addDialogVisible = true
      const { data: res } = await this.$axios.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.parentCat = res.data
    },
    cascaderChane () {
      if (this.cascaderValue.length !== 0) {
        this.addCatForm.cat_level = this.cascaderValue.length
        this.addCatForm.cat_pid = this.cascaderValue[this.cascaderValue.length - 1]
      } else {
        this.addCatForm.cat_level = 0
        this.addCatForm.cat_pid = 0
      }
      console.log(this.addCatForm)
      console.log(this.cascaderValue)
    },
    closeAddCateDialog () {
      this.$refs.addRuleForm.resetFields()
      this.cascaderValue = []
      this.addCatForm.cat_level = 0
      this.addCatForm.cat_pid = 0
    },
    addCateSubmit () {
      this.$refs.addRuleForm.validate(async flag => {
        if (!flag) return
        const { data: res } = await this.$axios.post('categories', this.addCatForm)
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        this.addDialogVisible = false
        this.getCatList()
      })
    }
  }
}
</script>

<style scoped lang='less'>
  .zk-table {
    margin-top: 10px;
  }

  .el-cascader {
    width: 100%;
  }
</style>
